package com.store.practice.service.impl;


import com.github.pagehelper.PageHelper;
import com.store.practice.mapper.BgDepartmentMapper;
import com.store.practice.mapper.BgDepartmentPositionRelationMapper;
import com.store.practice.mapper.BgPositionMapper;
import com.store.practice.mapper.BgStaffMapper;
import com.store.practice.model.*;
import com.store.practice.service.BgPositionService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class BgPositionServiceImpl implements BgPositionService {
    private static final Logger logger = LoggerFactory.getLogger(BgPositionServiceImpl.class);

    @Autowired
    private BgDepartmentMapper departmentMapper;
    @Autowired
    private BgPositionMapper positionMapper;
    @Autowired
    private BgDepartmentPositionRelationMapper departmentPositionRelationMapper;
    @Autowired
    private BgStaffMapper staffMapper;

    @Override
    public int insertPosition(Integer dptId, BgPosition position) {
        BgDepartment bgDepartment = departmentMapper.selectByPrimaryKey(dptId);
        if (bgDepartment==null){
            return -1;
        }
        int i = positionMapper.insert(position);
        BgDepartmentPositionRelation relation = new BgDepartmentPositionRelation();
        relation.setDptId(dptId);
        relation.setPosId(position.getId());
        departmentPositionRelationMapper.insert(relation);
        return i;
    }

    @Override
    public List<BgPosition> list(int pageNum,int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        return positionMapper.selectByExample(null);
    }

    @Override
    public int updatePosition(BgPosition position) {
        return positionMapper.updateByPrimaryKey(position);
    }

    @Override
    public int deletePosition(Integer posId) {
        BgDepartmentPositionRelationExample example = new BgDepartmentPositionRelationExample();
        example.createCriteria().andPosIdEqualTo(posId);
        Integer dptId = departmentPositionRelationMapper.selectByExample(example).get(0).getDptId();
        BgStaffExample staffExample = new BgStaffExample();
        staffExample.createCriteria().andDptIdEqualTo(dptId);
        List<BgStaff> bgStaffs = staffMapper.selectByExample(staffExample);
        String name = positionMapper.selectByPrimaryKey(posId).getName();
        if (bgStaffs!=null){
            for (BgStaff bgStaff:bgStaffs){
                if (bgStaff.getPosition().equals(name)){
                    return 10;
                }
            }
        }
        int i = positionMapper.deleteByPrimaryKey(posId);
        departmentPositionRelationMapper.deleteByExample(example);
        return i;
    }

    @Override
    public List<BgPosition> getByDptId(Integer dptId) {
        BgDepartmentPositionRelationExample example = new BgDepartmentPositionRelationExample();
        example.createCriteria().andDptIdEqualTo(dptId);
        List<BgDepartmentPositionRelation> relations = departmentPositionRelationMapper.selectByExample(example);
        List<Integer> ids = new ArrayList<>();
        if (relations!=null&&relations.size()>0){
            for (BgDepartmentPositionRelation relation:relations){
                ids.add(relation.getPosId());
            }
            BgPositionExample e = new BgPositionExample();
            e.createCriteria().andIdIn(ids);
            return positionMapper.selectByExample(e);
        }
        return null;
    }
}
